package com.nantai.jiafang.nantaijiafang.deviceManager.devicemanager;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.Place;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.ReasonBen;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.User;
import com.nantai.jiafang.nantaijiafang.deviceManager.Constans;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.Error;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.HttpClient;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.ResponseHandler;
import com.nantai.jiafang.nantaijiafang.deviceManager.preference.PreferencesUtil;
import com.readystatesoftware.viewbadger.BadgeView;

import java.util.List;
import java.util.Map;

import retrofit2.Call;

import static com.nantai.jiafang.nantaijiafang.R.id.Task;

public class OperateActivity extends AppCompatActivity implements View.OnClickListener{

    TextView mPutin;
    TextView mPutout;
    TextView mTask;
    TextView mRepair;
    TextView mCheck;
    TextView mKeep;
    Button out;
    int numberOfTask =0;
    BadgeView mBadgeView=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operate);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
        out=(Button)findViewById(R.id.logout_btn) ;
        out.setOnClickListener(this);
        mPutin=(TextView)findViewById(R.id.putin);
        mPutin.setOnClickListener(this);
        mKeep=(TextView)findViewById(R.id.keep);
        mKeep.setOnClickListener(this);
        mCheck=(TextView)findViewById(R.id.check);
        mCheck.setOnClickListener(this);
        mRepair=(TextView)findViewById(R.id.repair);
        mRepair.setOnClickListener(this);
        mTask=(TextView)findViewById(R.id.Task);
        mTask.setOnClickListener(this);
        mPutout=(TextView)findViewById(R.id.putout);
        mPutout.setOnClickListener(this);

        mBadgeView= new BadgeView(this, mTask);
        requestPlaceList();
        requestUserList();
        requestRepairReasonList();
        requestScrapReasonList();
        requestKeepReasonList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestNumberOfTask();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.putin: {
                startActivity(new Intent(OperateActivity.this, PutInActivity.class));
            }
            break;
            case R.id.putout: {
                startActivity(new Intent(OperateActivity.this, PutOutActivity.class));
            }
            break;
            case Task: {
                startActivity(new Intent(OperateActivity.this, TaskActivity.class));
            }
            break;
            case R.id.repair: {
                Intent intent = new Intent(OperateActivity.this, CaptureActivity.class);
                intent.putExtra("operate_type", String.valueOf(Constans.RepiarCode));
                startActivity(intent);
            }
            break;
            case R.id.check: {
                Intent intent = new Intent(OperateActivity.this, CaptureActivity.class);
                intent.putExtra("operate_type", String.valueOf(Constans.CheckCode));
                startActivity(intent);
            }
            break;
            case R.id.keep: {
                startActivity(new Intent(OperateActivity.this, KeepActivity.class));
            }
            break;
            case R.id.logout_btn:
                this.finish();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        return;
    }

    private void requestNumberOfTask() {
            HttpClient client = new HttpClient(OperateActivity.this.getApplicationContext());
            Call call = client.service().numberOfTask();
            client.request(call, new ResponseHandler<Map<String,String>>() {
                @Override
                public void onSuccess(Map<String,String> result) {
                    int number =  Integer.parseInt(result.get("count"));
                    numberOfTask=number;
                    runOnUiThread(new Runnable(){

                        @Override
                        public void run() {
                            //Toast.makeText(OperateActivity.this, String.valueOf(numberOfTask), Toast.LENGTH_SHORT).show();
                            if(numberOfTask!=0)
                            {
                                mBadgeView.setText(String.valueOf(numberOfTask));
                                mBadgeView.show();
                            }else
                            {
                                mBadgeView.setText("");
                                mBadgeView.hide();
                            }
                        }

                    });
                }
                @Override
                public void onFailure(int code, Error e) {
                    Toast.makeText(OperateActivity.this, "onFailure", Toast.LENGTH_SHORT).show();

                }
            });
    }

    private void requestUserList() {
            HttpClient client = new HttpClient(OperateActivity.this.getApplicationContext());
            Call call = client.service().getUser();
            client.request(call, new ResponseHandler<List<User>>() {
                @Override
                public void onSuccess(List<User> userList) {
                    PreferencesUtil.saveUserList(OperateActivity.this, userList);
                }

                @Override
                public void onFailure(int code, Error e) {


                }
            });
    }

    private void requestPlaceList() {
        HttpClient client = new HttpClient(OperateActivity.this.getApplicationContext());
        Call call = client.service().getAllPlace();
        client.request(call, new ResponseHandler<List<Place>>() {
            @Override
            public void onSuccess(List<Place> placeList) {

                PreferencesUtil.savePlaceList(OperateActivity.this, placeList);
            }

            @Override
            public void onFailure(int code, Error e) {

            }
        });
    }

    private void requestRepairReasonList() {
        HttpClient client = new HttpClient(OperateActivity.this.getApplicationContext());
        Call call = client.service().getAllRepairReason();
        client.request(call, new ResponseHandler<List<ReasonBen>>() {
            @Override
            public void onSuccess(List<ReasonBen> reasonBenList) {
                PreferencesUtil.saveRepairReasonList(OperateActivity.this, reasonBenList);
            }

            @Override
            public void onFailure(int code, Error e) {

            }
        });
    }
    private void requestScrapReasonList() {
        HttpClient client = new HttpClient(OperateActivity.this.getApplicationContext());
        Call call = client.service().getAllScrapReason();
        client.request(call, new ResponseHandler<List<ReasonBen>>() {
            @Override
            public void onSuccess(List<ReasonBen> reasonBenList) {

                PreferencesUtil.saveScrapReasonList(OperateActivity.this, reasonBenList);
            }

            @Override
            public void onFailure(int code, Error e) {
            }
        });
    }

    private void requestKeepReasonList() {
        HttpClient client = new HttpClient(OperateActivity.this.getApplicationContext());
        Call call = client.service().keepReasonList();
        client.request(call, new ResponseHandler<List<ReasonBen>>() {
            @Override
            public void onSuccess(List<ReasonBen> reasonBenList) {

                PreferencesUtil.saveKeepReasonList(OperateActivity.this, reasonBenList);
            }

            @Override
            public void onFailure(int code, Error e) {

            }
        });
    }

//    @OnClick(R.id.logout_btn)
//    public void onClick() {
//        Toast.makeText(this, "onclick", Toast.LENGTH_SHORT).show();
//        this.finish();
//    }
}
